package LeetCode.leetcode151;

import java.util.Arrays;
import java.util.Stack;

public class Main {
    public static void main(String[] args) {
        String s = "a good   example";
        System.out.println(Arrays.toString(s.split(" ")));
    }
    public static String reverseWords(String s) {
        Stack<String> stack = new Stack<>();
        String[] arr = s.split(" ");

        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < arr.length; i++) {
            stack.push(arr[i]);
        }
        for (int i = 0; i < arr.length; i++) {
            //如果这个字符串不是空串 全空格，才加入sb中
            if (stack.peek().trim().length() != 0){
                sb.append(stack.pop()+" ");
            }else {
                stack.pop();
            }
        }
        return sb.toString().trim();
    }

//    public String reverseWords2(String s) {
//        Stack<String> stack = new Stack<>();
//        String[] arr = s.split(" ");
//        StringBuilder sb = new StringBuilder();
//        for (int i = 0; i < arr.length; i++) {
//            stack.push(arr[i]);
//        }
//        for (int i = 0; i < arr.length; i++) {
//            sb.append(stack.pop()+" ");
//        }
//        return sb.toString().trim();
//    }
}
